package com.zzt.lotterysystem.service.activitystatus.operator;

import com.zzt.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.zzt.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.zzt.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.zzt.lotterysystem.service.enums.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-05-29
 * Time: 16:23
 */
@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if (null == activityId || null == prizeId || null == targetActivityStatus) {
            return false;
        }

        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByActivityIdAndPrizeId(activityId, prizeId);
        if (null == activityPrizeDO) {
            return false;
        }

        // 判断当前奖品状态是否 不是 COMPLETED
        if (activityPrizeDO.getStatus().equalsIgnoreCase(targetActivityStatus.name())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        ActivityStatusEnum targetActivityStatus = convertActivityStatusDTO.getTargetActivityStatus();
        try {
            activityPrizeMapper.updateStatus(activityId, prizeId, targetActivityStatus.name());
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
